package com.cn.jx.manager.service.query.Impl;

import java.util.ArrayList;
import java.util.List;

import com.cn.jx.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cn.jx.common.util.page.PageObj;
import com.cn.jx.manager.dao.OrgRepository;
import com.cn.jx.manager.po.Org;
import com.cn.jx.manager.po.User;
import com.cn.jx.manager.service.query.IOrgServiceQry;
import com.cn.jx.manager.service.query.IUserServiceQry;

import com.jx.jpaQuery.QueryCondition;
import com.jx.model.MenuEntry;
import com.jx.util.ArrayUtil;
import com.jx.util.Constant;
import com.jx.util.DateUtil;
import com.jx.util.MapObject;
import com.jx.util.Utility;

/**
 * 
 * @author 姜旭
 * @version v1.11.20.2019
 * @since 
 * <p>创建时间：2019年12月1日
 * <p>功能描述：
 * <p>官方网站：management/admin/index
 * <p>邮箱地址：524577360@qq.com
 */
@Service("orgServiceQry") 
@Transactional
public class OrgServiceImplQry extends BaseService implements IOrgServiceQry {
	
	@Autowired
	private OrgRepository orgDao;
	
	@Autowired
	private IUserServiceQry userServiceQry;
	
	/* 分页查询 */
	@Override
	@Transactional(readOnly = true)
	public PageObj<MapObject> findOrgByPageAndParams(QueryCondition cond) throws Exception {
		Specification<Org> speci = getSpecification(cond);
		Pageable pageable = getPageables(cond);
		Page<Org> puser = orgDao.findAll(speci, pageable);
		List<Org> list=puser.getContent();
		List<MapObject> listContent=new ArrayList<MapObject>();
		if(ArrayUtil.listNotEmpty(list)){
			list.forEach(o->{
				String leaderName="";
				if(Utility.isNotBlank(o.getLeader())){
					User uleader=userServiceQry.getUserById(o.getLeader());
					if(uleader!=null){
						leaderName=uleader.getChineseName();
					}
				}
				MapObject mp=MapObject.getInstance();
				mp.put("orgCode", o.getOrgCode());
				mp.put("orgName",o.getOrgName());
				mp.put("englishName", o.getEnglishName());
				mp.put("address", o.getAddress());
				mp.put("phone", o.getPhone());
				mp.put("leader", leaderName);
				mp.put("createTime", DateUtil.getDateFmt(o.getCreateTime()));
				mp.put("id", o.getId());
				listContent.add(mp);
			});
		}
		PageObj<MapObject> pageUser = new PageObj<MapObject>(puser, listContent);
		return pageUser;
	}

	/* 根据主键查询并加入缓存 */
	@Override
	@Transactional(readOnly = true)
//	@RedisCacheSave(key = "orgServiceQry", value = "'getOrgById='.concat(#orgId)", rTime = RedisTime.SIX_HOURS, cls = Org.class)
	public Org getOrgById(String orgId) {
		Org o = findEntityByPkid(orgId, Org.class);
		return o;
	}

	/* 查询组织树 */
	@Override
	@Transactional(readOnly = true)
//	@RedisCacheSave(key = "orgServiceQry", value = "'findOrgTree'", rTime = RedisTime.SIX_HOURS, cls = MenuEntry.class)
	public List<MenuEntry> findOrgTree(String parent) {
		String parentroot = Utility.isBlank(parent) ? Constant._ROOT : parent;
		List<MenuEntry> result = new ArrayList<MenuEntry>();
		List<Org> rootlist = orgDao.findAllOrgByParent(parentroot);
		if(ArrayUtil.listNotEmpty(rootlist)){
			rootlist.forEach(og->{
				MenuEntry mroot = new MenuEntry();
				mroot.setText(og.getOrgName());
				mroot.setCode(og.getOrgCode());
				mroot.setId(og.getId());
				mroot.setType(og.getParentId());
				mroot.setLeader(og.getLeader());
				List<MenuEntry> childlist = findOrgTree(og.getId());
				mroot.setChildren(childlist);
				result.add(mroot);
			});
		}
		return result;
	}

	@Override
	@Transactional(readOnly = true)
//	@RedisCacheSave(key = "orgServiceQry", value = "'findLeaderIdByOrg='.concat(#orgId)", rTime = RedisTime.SIX_HOURS, cls = String.class)
	public String findLeaderIdByOrg(String orgId) {
		Org o=getOrgById(orgId);
		if(o!=null) {
			return o.getLeader();
		}
		return Constant._EMPTY;
	}
}
